﻿#ifndef _OI_PLSCANNERCORE_WORKER_HPP_
#define _OI_PLSCANNERCORE_WORKER_HPP_
#include "LT360Base/OiDataStructure.hpp"
#include "LT360Base/OiAlgorithms.hpp"
#include "LT360Base/OiChart.hpp"
#include "PLScannerCore/OiPLScannerCore.hpp"
#include "Config/OiProductParams.hpp"
#include "Camera/OiLT360CameraDriver.hpp"
namespace Oi 
{ 
    namespace PLScannerCore 
    {
        namespace Environment
        {
            extern bool _startTask;
            extern bool _newParams;
        }

        namespace WorkResult
        {
            enum ResultCode
            {
                RC_Success = 1,
                RC_ProfileEmpty = 100,

                RC_Warning = 200,
                RC_Error = 300,
                RC_Formualr_Disable = 301,
                RC_End = 302,
            };

            class OI_PLSCANNER_CORE_API Result
            {
            public:
                int result;
                OiResultData data;
                OiProfile profile1;
                OiProfile profile2;
                ProductParams params;

            public:
                Result();

                Result(const Result& p);

                Result& operator=(const Result& p);

            public:
                static void caculateResult(Result & result, bool dir, void* data);

                static void saveResultToDataBase(const Result & result);

                static void saveResultToFile(const Result & result, const QString path);

                static void loadResultFromDataBase(Result & result, const int id);

                static void loadResultFromFile(Result & result, const QString path);
            };
        }

        class OI_PLSCANNER_CORE_API WorkLoopModeTask : public QThread
        {
            Q_OBJECT

        public:
            WorkLoopModeTask(void* impl, QObject* parent = nullptr);
            void run();

        signals:
            void enterRound();
            void leaveRound();

        private:
            OI_DECLARE_IMPL(WorkLoopModeTask);
            OI_DISABLE_COPY(WorkLoopModeTask);
        };

     
        class OI_PLSCANNER_CORE_API PLScannerWorker : public QWidget
        {
            Q_OBJECT

        public:
            PLScannerWorker(QWidget* parent = 0);
            ~PLScannerWorker();

        public:
            void setParams(const ProductParams& params);
            int isRunning() const;

        public:
            WorkResult::Result result();
            const std::vector<QPointF>& msrResult() const;
            double value();
        signals:
            void enterRound();
            void leaveRound();
            void enterTask();
            void leaveTask();
            void profileEmpty();

        public slots:
            void start(int mode = 0);
            void stop();
            void onSnapRefresh(const LTCameraFrame&);
            void onRefresh(const LTCameraFrame&, const QDateTime& timestamp);
            void onUpdateChart();
        public slots:
            void onLeaveTask();

        public slots:
            void product();
            void save(const QString& path);
            void open(const QString& file);
            void onMatch(const QList<QPointF>& points);
            void onMatchDone();
            void onReStartCamera();
            void measureDone(
                const LTCameraFrame& rangeMaps,
                const QDateTime& timestamp,
                const QList<QPointF>& profile,
                const std::vector<QPointF>& outputs,
                const Math::Matrix33f& currentPose);
            void measureDone();
            QList<QList<QPointF>> getUpdateProfile(QList<QList<QPointF>> parsedpoints, int leftCcenter, int rightCenter);

            bool isTempl() const;

        private:
            OI_DECLARE_IMPL(PLScannerWorker);
            OI_DISABLE_COPY(PLScannerWorker);
        };
    } 
}

#endif
